package com.csq.portal.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csq.cms.service.RedisService;
import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.common.util.LoginUtil;
import com.csq.manager.pojo.Account;
import com.csq.manager.service.AccountService;

@Controller
@RequestMapping("/portal/account")
public class AccountController {

	@Autowired
	private RedisService redisService;

	@Autowired
	private AccountService accountService;

	@RequestMapping(value = "/index")
	public String index(String username, HttpServletRequest request) {

		// 从 redis 获取 session

		Account account = redisService.getRedisAccount(username);

		if (account == null) {
			return "redirect:http://localhost:8080/sso-web/web/account/login";
		}

		request.getSession().setAttribute("account", account);
		return "/web/account/index";
	}

	@RequestMapping(value = "/editPassword", method = RequestMethod.POST)
	public String editPassword(String oldpass, String userpass, String email,
			HttpServletRequest request) {

		ReturnResult checkAccountLogin = LoginUtil.checkAccountLogin(request);
		if (checkAccountLogin.getState() != StatusCode.SUCCESS) {
			return checkAccountLogin.getMessage();
		}

		// 设置要更新的用户 id
		Account redisAccount = (Account) request.getSession().getAttribute(
				"account");

		//
		Account accountByID = accountService.getAccountByID(redisAccount
				.getUserId());

		if (!email.equals(accountByID.getEmail())
				&& !DigestUtils.md5DigestAsHex(oldpass.getBytes()).equals(
						accountByID.getUserpass())) {

			ReturnResult returnResult = ReturnResult.build(
					StatusCode.WRONGPASSWORD, "WRONGPASSWORD");
			request.setAttribute("returnResult", returnResult);
			return "/web/error/index";
		}

		redisAccount
				.setUserpass(DigestUtils.md5DigestAsHex(userpass.getBytes()));

		ReturnResult updateAccount = accountService.updateAccount(redisAccount);

		if (updateAccount.getState() != StatusCode.SUCCESS) {
			request.setAttribute("returnResult", updateAccount);
			return "/web/error/index";
		}

		return "/web/account/edit/Password";
	}

	@RequestMapping(value = "/editAccount", method = RequestMethod.POST)
	public String editAccount(Account account, HttpServletRequest request) {

		ReturnResult checkAccountLogin = LoginUtil.checkAccountLogin(request);
		if (checkAccountLogin.getState() != StatusCode.SUCCESS) {
			return checkAccountLogin.getMessage();
		}

		System.out.println("post account:" + account);

		// 设置要更新的用户 id
		account.setUserId(((Account) checkAccountLogin.getObject()).getUserId());

		ReturnResult updateAccount = accountService.updateAccount(account);

		if (updateAccount.getState() != StatusCode.SUCCESS) {

			return "/web/error/index";
		}

		Account accountByID = accountService
				.getAccountByID(((Account) checkAccountLogin.getObject())
						.getUserId());
		System.out.println("after update:" + accountByID);

		request.getSession().setAttribute("account", accountByID);

		return "/web/account/editAccount";
	}

	@RequestMapping(value = "/verifyAccount")
	public String verifyAccount(Account account, HttpServletRequest request) {

		ReturnResult checkAccountLogin = LoginUtil.checkAccountLogin(request);
		if (checkAccountLogin.getState() != StatusCode.SUCCESS) {
			return checkAccountLogin.getMessage();
		}

		Account redisAccount = (Account) request.getSession().getAttribute(
				"account");
		if (account.getUserId() != null) {
			// 设置要更新的用户 id
			account.setUserId(redisAccount.getUserId());

			ReturnResult updateAccount = accountService.updateAccount(account);

			if (updateAccount.getState() != StatusCode.SUCCESS) {

				return "/web/error/index";
			}
		}

		Account accountByID = accountService.getAccountByID(redisAccount
				.getUserId());
		request.setAttribute("accountByID", accountByID);

		return "/web/account/verifyAccount";
	}

	@RequestMapping(value = "/verify")
	public String verify(HttpServletRequest request) {
		return "/web/account/verifyAccount";
	}
}
